﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ListItemEditor.UI
{
    public partial class FarmExplorer : UserControl
    {
        public enum ExplorerMode { None, Farm, WebSvc }

        public FarmExplorer()
        {
            InitializeComponent();
            this.Mode = ExplorerMode.None;
        }

        public event EventHandler<TreeViewEventArgs> TreeNodeOpen;

        public ExplorerMode Mode { get; private set; }
        public GridViewDataSourceSettings LastSettings { get; private set; }

        private TreeViewDataSource _treeViewDataSource;

        private void FarmExplorer_Load(object sender, EventArgs e)
        {
        }

        public ListSiteMapNode CurrentListNode
        {
            get
            {
                if (this.treeView1.SelectedNode == null) return null;
                return this.treeView1.SelectedNode.Tag as ListSiteMapNode;
            }
        }

        public TreeNode CurrentTreeNode
        {
            get
            {
                return this.treeView1.SelectedNode;
            }
        }

        public void LoadExplorer()
        {
            if (this.Mode == ExplorerMode.Farm) return;
            this.Mode = ExplorerMode.Farm;

            SetTreeViewDataSource(new TreeViewDataSource(new ListSiteMapProvider(), this.treeView1));
        }

        public void LoadWSvcExplorer(GridViewDataSourceSettings settings, bool forceReload)
        {
            if (!forceReload && this.Mode == ExplorerMode.WebSvc) return;
            this.Mode = ExplorerMode.WebSvc;
            this.LastSettings = settings;

            SetTreeViewDataSource(new TreeViewDataSource(new ListSiteMapProviderWSvc(settings), this.treeView1));
        }

        public void RefreshNode(TreeNode node)
        {
            if (this._treeViewDataSource == null) return;
            this._treeViewDataSource.RefreshNode(node);
        }

        public TreeNode FindTreeNode(string key)
        {
            if (this._treeViewDataSource == null) return null;
            return this._treeViewDataSource.FindTreeNode(key);
        }

        public void SelectNode(TreeNode node)
        {
            this.treeView1.SelectedNode = node;
        }

        private void SetTreeViewDataSource(TreeViewDataSource newSource)
        {
            if (_treeViewDataSource != null) _treeViewDataSource.Disconnect();

            _treeViewDataSource = newSource;
            _treeViewDataSource.TreeNodeDataBound += new EventHandler<TreeViewDataSource.TreeViewDataSourceEventArgs>(_treeViewDataSource_TreeNodeDataBound);
            _treeViewDataSource.ShowRoot = false;
            _treeViewDataSource.DataBind();
        }

        private void _treeViewDataSource_TreeNodeDataBound(object sender, TreeViewDataSource.TreeViewDataSourceEventArgs e)
        {
            TreeNode node = e.TreeNode;
            ListSiteMapNode siteMapNode = (ListSiteMapNode)node.Tag;
            node.ImageIndex = node.SelectedImageIndex = (int)siteMapNode.NodeData.NodeType - 1;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            
        }

        private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (treeView1.SelectedNode == null) return;
            FireTreeNodeOpen(treeView1.SelectedNode);
        }

        private void FireTreeNodeOpen(TreeNode node)
        {
            if (TreeNodeOpen != null) TreeNodeOpen(this, new TreeViewEventArgs(node));
        }

        private void treeView1_MouseClick(object sender, MouseEventArgs e)
        {
            if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
            {
                TreeViewHitTestInfo hi = this.treeView1.HitTest(e.X, e.Y);
                if ((hi.Node != null) && (hi.Node.Tag != null))
                {
                    TreeNode node = hi.Node;
                    ContextMenuStrip menu = new ContextMenuStrip();
                    ToolStripMenuItem mi = new ToolStripMenuItem();
                    mi.Text = "refresh";
                    mi.Tag = node;
                    menu.Items.Add(mi);

                    EventHandler handler = delegate(object s, EventArgs args)
                    {
                        ToolStripMenuItem item = s as ToolStripMenuItem;
                        if (item != null)
                        {
                            this._treeViewDataSource.RefreshNode(mi.Tag as TreeNode);
                        }
                    };
                    mi.Click += new EventHandler(handler);

                    ListSiteMapNode listNode = node.Tag as ListSiteMapNode;
                    if (listNode.NodeData.NodeType == ListSiteMapNodeType.List || listNode.NodeData.NodeType == ListSiteMapNodeType.View)
                    {
                        mi = new ToolStripMenuItem();
                        mi.Text = "open";
                        mi.Tag = node;
                        menu.Items.Add(mi);

                        handler = delegate(object s, EventArgs args)
                        {
                            ToolStripMenuItem item = s as ToolStripMenuItem;
                            if (item != null)
                            {
                                TreeNode treeNode = mi.Tag as TreeNode;
                                this.treeView1.SelectedNode = treeNode;
                                this.FireTreeNodeOpen(treeNode);
                            }
                        };
                        mi.Click += new EventHandler(handler);
                    }

                    menu.Show(this.treeView1, new Point(e.X, e.Y));
                }
            }
        }
    }
}